<?php

declare(strict_types=1);
/**
 * 资讯功能
 * User：Abell
 * Date：2020/6/16
 */

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Controller\AbstractController;
use App\Service\ArticleService;
use Hyperf\Di\Annotation\Inject;
use App\Utils\ApiUtils;
use App\Validate\ArticleValidation;

class ArticleController extends AbstractController
{
    /**
     * @Inject
     * @var ArticleService
     */
    protected $service;

    /**
     * 资讯删除
     * @Author abell
     * @DateTime 2020-06-16
     * @param array $request
     * @return
     */
    public function delete(){
        make(ArticleValidation::class)->check($this->request->all(),'edit_show')->response();
        $id = $this->request->input('id');
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'ok',$this->service->editSave(['id'=>$id,'is_del'=>0]),1));
    }

    /**
     * 资讯状态修改
     * @Author abell
     * @DateTime 2020-06-16
     * @param array $request
     * @return
     */
    public function change(){
        make(ArticleValidation::class)->check($this->request->all(),'change')->response();
        $id = $this->request->input('id');
        $status = $this->request->input('status');
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'ok',$this->service->editSave(['id'=>$id,'status'=>$status]),1));
    }

    /**
     * 资讯编辑
     * @Author abell
     * @DateTime 2020-06-16
     * @param array $request
     * @return
     */
    public function edit(){
        if($this->request->isMethod('get')){
            make(ArticleValidation::class)->check($this->request->all(),'edit_show')->response();
            $result = $this->service->editShow($this->request->input('id'));
        }else{
            make(ArticleValidation::class)->check($this->request->all(),'edit_save')->response();
            $result = $this->service->editSave($this->request->all());
        }
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'ok',$result,1));
    }

    /**
     * 资讯新增
     * @Author abell
     * @DateTime 2020-06-16
     * @param array $request
     * @return
     */
    public function add(){
        make(ArticleValidation::class)->check($this->request->all(),'add')->response();
        $result = $this->service->add($this->request->all());
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'ok',$result,1));
    }

    /**
     * 资讯列表
     * @Author abell
     * @DateTime 2020-06-16
     * @param array $request
     * @return
     */
    public function list(){
        $page = $this->request->input('page',1);
        $pageSize = $this->request->input('pageSize',15);
        $order = $this->request->input('order','add_time desc');
        $result = $this->service->list($page,$pageSize,$order);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'ok',$result['data'],$result['total']));
    }
}